package cn.sc.allinpay.allfavour.business.common.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;

import java.math.BigDecimal;

import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.R;
import cn.sc.allinpay.allfavour.base.widget.CashierInputFilter;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponData;

/**
 * description：验券卡券金额输入弹出框
 * author：pany
 * on 2018/7/28 17:36
 */
public class CheckInputAmtDialog extends Dialog implements View.OnClickListener {

    private CheckCouponData data;
    private String maxAmt;//最大可用金额
    private AppCompatTextView tvUseAmt;
    private AppCompatEditText etAmt;
    private OnDialogListener onDialogListener;

    public CheckInputAmtDialog(@NonNull Context context, CheckCouponData data) {
        super(context);
        this.data = data;
        this.maxAmt = new BigDecimal(AmtUtils.changeF2Y(data.getBalance())).toString();
    }

    public CheckInputAmtDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_check_input_amt);
        tvUseAmt = findViewById(R.id.tvUseAmt);
        tvUseAmt.setText("剩余可用金额(￥):  " + AmtUtils.changeF2Y(data.getBalance()));
        etAmt = findViewById(R.id.etAmt);
        etAmt.setText(data.getUseAmt());
        InputFilter[] filters = {new CashierInputFilter(maxAmt)};
        etAmt.setFilters(filters);
        etAmt.setSelection(etAmt.getText().length());
        findViewById(R.id.btnConfirm).setOnClickListener(this);
        findViewById(R.id.btnCancel).setOnClickListener(this);
    }

    public void setOnDialogListener(OnDialogListener onDialogListener) {
        this.onDialogListener = onDialogListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnConfirm:
                dismiss();
                if (onDialogListener != null) {
                    String amt = etAmt.getText().toString().trim();
                    if (TextUtils.isEmpty(amt) || BigDecimal.ZERO.compareTo(new BigDecimal(amt)) == 0) {
                        onDialogListener.onConfirm(null);
                    } else {
                        onDialogListener.onConfirm(amt);
                    }
                }
                break;
            case R.id.btnCancel:
                dismiss();
                if (onDialogListener != null) {
                    onDialogListener.onCancel();
                }
                break;
        }
    }

    public static abstract class OnDialogListener {

        protected abstract void onConfirm(String amt);

        protected void onCancel() {
        }
    }
}
